import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class AdminMenuPanel extends JPanel{





		JButton schedule=new JButton();
		//JButton drop=new JButton();
		JButton logout=new JButton();
		//JButton transcript=new JButton();
		//JButton account=new JButton();
		JButton student=new JButton();
		JButton finance=new JButton();
		JButton notification=new JButton();
		JPanel panel=new JPanel();
		Image img;
		static String SID;
		static String fname="username",lname="last";
		
		public AdminMenuPanel(){
			
			img=new ImageIcon("menu_background.png").getImage();
			final ImageIcon regi=new ImageIcon("admincourses_button.png");
			final ImageIcon log=new ImageIcon("logout_button.png");
			final ImageIcon stu=new ImageIcon("adminstudent_button.png");
			final ImageIcon fin=new ImageIcon("adminfinances_button.png");
			final ImageIcon cregi=new ImageIcon("admincourses_cbutton.png");
			final ImageIcon clog=new ImageIcon("logout_cbutton.png");
			final ImageIcon cstu=new ImageIcon("adminstudent_cbutton.png");
			final ImageIcon cfin=new ImageIcon("adminfinances_cbutton.png");
			final ImageIcon not=new ImageIcon("adminnotification_button.png");
			final ImageIcon cnot=new ImageIcon("adminnotification_cbutton.png");
			
			
			
			button(schedule,regi);
			button(logout,log);
			//button(transcript,trans);
			button(student,stu);
			button(finance,fin);
			button(notification,not);
			//button(currents,current);
			//button(drop,dro);
			
			
			
			panel.setPreferredSize(new Dimension(600,400));
			panel.setBackground(Color.white);
			
			
			panel.setOpaque(false);
			panel.setLayout(new GridBagLayout());
			
			setOpaque(false);
			
			String u=LoginPanel.uid;
			
			try{
				String dName = "com.mysql.jdbc.Driver";
				String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
				Connection con = DriverManager.getConnection(url);
				java.sql.Statement mysqlStatement = con.createStatement();
				java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT fname, lname, SID FROM student_list WHERE username='"+u+"'");
				while (resultSet.next()) {
				fname= resultSet.getString(1);
				lname= resultSet.getString(2);
				SID=resultSet.getString(3);
				}
				con.close();
				}
				catch(Exception ex){}
			
			
			JLabel welcome=new JLabel("Welcome, "+fname+" "+lname+"!");
			JLabel l=new JLabel("");
			
			JPanel p=new JPanel();
			p.setLayout(new GridLayout(9,1));
			p.add(welcome);
			p.add(l);
			p.add(student);
			p.add(schedule);
			p.add(finance);
			p.add(notification);
			p.add(logout);

			p.setOpaque(false);
			
			panel.add(p);
			
			
			
			//panel.setLayout(new BorderLayout());
		
			setLayout(new BorderLayout());
			add(panel,BorderLayout.PAGE_START);
			
			
			
			student.addActionListener(new ActionListener(){
	            public void actionPerformed(ActionEvent e){
	                remove(panel);
	                add(new AdminStudentPanel());
	                validate();
	                repaint();
	            }
	    });
			student.setContentAreaFilled(false);
			student.setIcon(stu);
			student.setRolloverIcon(cstu);
			student.setPressedIcon(cstu);
			student.getModel().addChangeListener(new ChangeListener() {
		        @Override
		        public void stateChanged(ChangeEvent e) {
		            ButtonModel model = (ButtonModel) e.getSource();
		            if (model.isRollover()) {
		            	student.setIcon(cstu);
		            } else {
		            	student.setIcon(stu);
		            }// etc ...//  if (model.isPressed()) {
		         }
		    });
			
			 logout.addActionListener(new ActionListener(){
	             public void actionPerformed(ActionEvent e){
	                     remove(panel);
	                 add(new LoginPanel());
	                 validate();
	                 repaint();
	                 JOptionPane.showMessageDialog(null, "You've successfully logged out");
	             }
	     });
			
			 logout.setContentAreaFilled(false);
			 logout.setIcon(log);
			 logout.setRolloverIcon(clog);
			 logout.setPressedIcon(clog);
			 logout.getModel().addChangeListener(new ChangeListener() {
			        @Override
			        public void stateChanged(ChangeEvent e) {
			            ButtonModel model = (ButtonModel) e.getSource();
			            if (model.isRollover()) {
			            	logout.setIcon(clog);
			            } else {
			            	logout.setIcon(log);
			            }// etc ...//  if (model.isPressed()) {
			         }
			    });
	    
	schedule.addActionListener(new ActionListener(){
	        public void actionPerformed(ActionEvent e){
	            
	            remove(panel);
	            add(new AdminRegisterPanel());
	            validate();
	            repaint();
	            
	        }
	});
	schedule.setContentAreaFilled(false);
	schedule.setIcon(regi);
	schedule.setRolloverIcon(cregi);
	schedule.setPressedIcon(cregi);
	schedule.getModel().addChangeListener(new ChangeListener() {
	        @Override
	        public void stateChanged(ChangeEvent e) {
	            ButtonModel model = (ButtonModel) e.getSource();
	            if (model.isRollover()) {
	            	schedule.setIcon(cregi);
	            } else {
	            	schedule.setIcon(regi);
	            }// etc ...//  if (model.isPressed()) {
	         }
	    });
	finance.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            
            remove(panel);
            add(new AdminFinancePanel());
            validate();
            repaint();
            
        }
});
	finance.setContentAreaFilled(false);
	finance.setIcon(fin);
	finance.setRolloverIcon(cfin);
	finance.setPressedIcon(cfin);
	finance.getModel().addChangeListener(new ChangeListener() {
	        @Override
	        public void stateChanged(ChangeEvent e) {
	            ButtonModel model = (ButtonModel) e.getSource();
	            if (model.isRollover()) {
	            	finance.setIcon(cfin);
	            } else {
	            	finance.setIcon(fin);
	            }// etc ...//  if (model.isPressed()) {
	         }
	    });
	notification.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            
            remove(panel);
            add(new AdminNotification());
            validate();
            repaint();
            
        }
});
	notification.setContentAreaFilled(false);
	notification.setIcon(not);
	notification.setRolloverIcon(cnot);
	notification.setPressedIcon(cnot);
	notification.getModel().addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            ButtonModel model = (ButtonModel) e.getSource();
            if (model.isRollover()) {
            	notification.setIcon(cnot);
            } else {
            	notification.setIcon(not);
            }// etc ...//  if (model.isPressed()) {
         }
    });
		
	    
		}
		
		
		
			//We'll set more appropriate layout later ex) gridlayout etc..
		
		@Override
	    public void paintComponent(Graphics g) {
			super.paintComponents(g);
	        g.drawImage(img, 0, 0, this);
	    }
			
		
		private void button(JButton b, ImageIcon i){
			b.setBorderPainted(false);
			b.setBorder(null);
			//button.setFocusable(false);
			b.setMargin(new Insets(0, 0, 0, 0));
			b.setContentAreaFilled(false);
			b.setIcon(i);
			//b.setRolloverIcon(myIcon2);
			//b.setPressedIcon(myIcon3);
		}
		
	}
		
		



